{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###### Content provided under a Creative Commons Attribution license CC-BY 4.0; code under BSD 3-Clause license. (c)2015 L.A. Barba, Pi-Yueh Chuang."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2D Multi-element airfoil"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This is your [_AeroPython_](https://github.com/barbagroup/AeroPython) assignment for the fourth course module, titled **\"Vortex-panel method for lifting bodies.\"** You will investigate a 2D multi-element airfoil, or wing section with extended flap, adapting the source-vortex panel method of [Lesson 11](11_Lesson11_vortexSourcePanelMethod.ipynb) to compute the flow around two airfoils. \n",
    "\n",
    "The key concepts for using the vortex-source panel method with a multi-element airfoil are:\n",
    "\n",
    "1. each airfoil has its own vortex strength, and\n",
    "2. the trailing edge at each airfoil must follow the Kutta condition.\n",
    "\n",
    "In the first part of the assignment, you will use a test for which we have a theoretical solution, due to Williams (1973). Have a look at the reference, and get a feel for its mathematical genius! In the second part, you will use a more popular airfoil (the NACA 23012) for both the main wing section and the flap. For this case, there is an old classic NACA report by Wenzinger (1938) giving data from experiments in a wind tunnel. This exercise will get you wondering about some very interesting aerodynamical questions!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Part 1: Williams' test case"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Your task is to calculate the pressure coefficient on a multi-element wing section, consisting of a *main airfoil* and an *external-airfoil flap*. This test is from Williams (1973), who obtained a beautiful theoretical solution using the techniques of conformal mapping.\n",
    "\n",
    "The profile of the wing section looks like this, with flap extended:\n",
    "<center> <img src=\"resources/MultiElementAirfoil.png\" width=\"500\"> </center>\n",
    "\n",
    "We provide CSV files with the $x$ and $y$ coordinates for the end nodes of each panel; these files are in the `resources` directory of our GitHub repository. The files have the following naming convention, where `***` represents the value of `N`, the number of panels: `MainFoil_N=***.csv`, `FlapFoil_N=***.csv`.\n",
    "\n",
    "You can use fewer panels for debugging while developing your codes and then use more panels for more serious calculations when answering the questions in this assignment.\n",
    "\n",
    "There are also two files named `Cp_Main_theoretical.csv` and `Cp_Flap_theoretical.csv` containing the theoretical values of pressure coefficient versus $x$-coordinates, for the flap configuration we give you in the data files (a $30$º flap deflection, with the main airfoil at $0$º angle of attack).\n",
    "\n",
    "The theoretical lift and drag force for potential flow over this 2D wing section with zero angle of attack are $3.7386$ and $0$, respectively (non-dimensionalized by dynamic pressure).\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Hints"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Recall the [exercise in Lesson 11](11_Lesson11_Exercise.ipynb), which asks you to derive the mathematical formulation of the vortex-source panel method. \n",
    "\n",
    "Now, for the 2-element airfoil, you need to derive the required mathematical expressions, following the same process as in the exercise, starting from the following expression for the potential:\n",
    "\n",
    "$$\n",
    "\\begin{split}\n",
    "\\phi(x, y) &= U_{\\infty}x\\cos\\alpha + U_{\\infty}y\\sin\\alpha \\\\\n",
    "& + \\int_{main} \\frac{1}{2\\pi} \\sigma(s) \\ln \\sqrt{(x-\\xi(s))^2+(y-\\eta(s))^2} ds  \\\\\n",
    "& + \\int_{flap} \\frac{1}{2\\pi} \\sigma(s) \\ln \\sqrt{(x-\\xi(s))^2+(y-\\eta(s))^2} ds \\\\\n",
    "& - \\int_{main} \\frac{1}{2\\pi} \\gamma(s) \\tan^{-1} \\frac{y-\\eta(s)}{x-\\xi(s)} ds  \\\\\n",
    "& - \\int_{flap} \\frac{1}{2\\pi} \\gamma(s) \\tan^{-1} \\frac{y-\\eta(s)}{x-\\xi(s)} ds\n",
    "\\end{split}\n",
    "$$\n",
    "\n",
    "Assume the following:\n",
    "\n",
    "1. $\\sigma(s)$ is constant on each panel, \n",
    "2. $\\gamma(s)$ is constant on each airfoil, and\n",
    "3. the 1<sup>st</sup> to $N_a$<sup>th</sup> panels are on the main airfoil and the $(N_a+1)$<sup>th</sup> to $N$<sup>th</sup> panels are on the flap (where $N=N_a+N_b$, and $N_b$ is the number of panels on the flap).\n",
    "\n",
    "There will be $N+2$ unknowns, that is, $\\sigma_1\\cdots\\sigma_N$, $\\gamma_a$, and $\\gamma_b$. The values of $\\gamma_a$, and $\\gamma_b$ represent the vortex strengths on the main and flap airfoils, respectively.\n",
    "\n",
    "You should be able to obtain the following matrix forms of normal and tangential velocity on the $i$<sup>th</sup> panel:\n",
    "\n",
    "$$\n",
    "U^n_i = b^n_i + \n",
    "\\left[\\begin{smallmatrix}A^n_{i1}\\cdots A^n_{iN}\\end{smallmatrix}, \\sum_{j=1}^{N_a}B^n_{ij}, \\sum_{j=N_a+1}^{N}B^n_{ij}\\right]\n",
    "\\left[\\begin{smallmatrix}\\sigma_1 \\\\ \\vdots \\\\ \\sigma_N \\\\ \\gamma_a \\\\ \\gamma_b\\end{smallmatrix}\\right]\n",
    "$$\n",
    "\n",
    "$$\n",
    "U^t_i = b^t_i + \n",
    "\\left[\\begin{smallmatrix}A^t_{i1}\\cdots A^t_{iN}\\end{smallmatrix}, \\sum_{j=1}^{N_a}B^t_{ij}, \\sum_{j=N_a+1}^{N}B^t_{ij}\\right]\n",
    "\\left[\\begin{smallmatrix}\\sigma_1 \\\\ \\vdots \\\\ \\sigma_N \\\\ \\gamma_a \\\\ \\gamma_b\\end{smallmatrix}\\right]\n",
    "$$\n",
    "\n",
    "Using the non-penetration conditions on the $1$<sup>st</sup> to $N$<sup>th</sup> panels, you will have $N$ linear equations. To solve for the $N+2$ unknowns, the extra two linear equations you need are the Kutta conditions on the two airfoils:\n",
    "\n",
    "$$\n",
    "U^t_{1} = U^t_{N_a} \\\\\n",
    "U^t_{N_a+1} = U^t_{N}\n",
    "$$\n",
    "\n",
    "And you are ready to solve for potential flow around a 2D multi-component wing!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Questions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Run your codes using 100 panels on both main and flap airfoils and answer the following numeric-value questions."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Q1."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* What is the pressure coefficient on the $3$<sup>rd</sup> panel on the *main airfoil*?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Q2."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* What is the pressure coefficient on the $10$<sup>th</sup> panel on the *flap airfoil*?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Q3."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* What is the value of lift?\n",
    "\n",
    "**Hint:**  $~L = - \\oint_{main}p\\vec{n}\\cdot\\vec{j}dl- \\oint_{flap}p\\vec{n}\\cdot\\vec{j}dl$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Q4."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* What is the value of drag?  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Q5."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* Run the program again with 200 panels on both main and flap airfoils. What is the percentage of improvement of the absolute error in the value of lift?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Part 2: Wenzinger's test case"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You should now have a usable panel solver for 2-component airfoils. Next, you'll change the target airfoil to a more realistic profile. There are experimental results available (Wenzinger, 1938) for a NACA 23012 airfoil with a NACA23012 external-airfoil flap, which we'll compare with.\n",
    "\n",
    "The following figure shows the profile of this wing section with 0º flap angle:\n",
    "\n",
    "<center><img src=\"resources/NACA23012withFlap.png\" width=500> </center>\n",
    "\n",
    "We provide CSV files for the endpoints of a panel discretization of this wing section, using 150 panels on each airfoil: `NACA23012_MainFoil.csv` and `NACA23012_FlapFoil.csv` in the `resources` folder.\n",
    "\n",
    "The flap has a rotating center (hinge) at the location $(1.03, -0.054)$ (see the configuration on Figure 1 of the reference). Using coordinate rotation, you can obtain different configurations with different flap deflection angles.\n",
    "\n",
    "Try to use different flap deflection angles and different angles of attack (of the main airfoil) and compare to Wenzinger's experimental results. Note that the definition of the total lift coefficient is $L/(l_{main}+l_{flap})$, where $L$ is the lift force (per unit span) like in the previous problem, and $l_{main}$ and $l_{flap}$ are the chord lengths of the two airfoils. **Think about what could be the sources of difference between your results and the experimental data**."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Questions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Q1."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* Use the [bisection method](http://en.wikipedia.org/wiki/Bisection_method) to determine the angle of attack, $\\alpha$, that will cause zero lift when the flap angle is $20$º. Round your answer to the second digit after the floating point.\n",
    "\n",
    "**HINT**: you can start the bisection method within the range $-14\\le\\alpha\\le14$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Q2."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* Use the [bisection method](http://en.wikipedia.org/wiki/Bisection_method) to determine the flap angle that will cause zero lift when the angle of attack, $\\alpha$, is $0$º. Round your answer to the second digit after the floating point."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Q3."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* Suppose that the main airfoil is attached to the fuselage of an airplane at an incidence angle of $4$º (that is, the built-in angle of attack of the wing when the airplane is in level flight is $4$º). Compute the lift coefficient with the flap at $0$º deflection, and at $5$, $10$ and $15$ degrees. How much slower (in percentage) can the airplane fly (in level flight) with the flaps extended at these settings, compared with $0$º?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Think"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Think about the meaning of what you observed in **Q3**, above. What is the effect of using the flap?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## References"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*  B. R. Williams (1973), *An Exact Test Case for the Plane Potential Flow About Two Adjacent Lifting Aerofoils*, Reports & Memoranda No. 3717, Aeronautical Research Council of the United Kingdom // [PDF at Cranfield University](http://naca.central.cranfield.ac.uk/reports/arc/rm/3717.pdf)\n",
    "\n",
    "* C. J. Wenzinger (1938), *Pressure distribution over an NACA 23012 airfoil with an N.A.C.A. 23012 external-airfoil flap*, NACA Technical Report No.614 // [PDF at NASA](http://ntrs.nasa.gov/archive/nasa/casi.ntrs.nasa.gov/19930091690.pdf)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "-----------------------------"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<link href='http://fonts.googleapis.com/css?family=Fenix' rel='stylesheet' type='text/css'>\n",
       "<link href='http://fonts.googleapis.com/css?family=Alegreya+Sans:100,300,400,500,700,800,900,100italic,300italic,400italic,500italic,700italic,800italic,900italic' rel='stylesheet' type='text/css'>\n",
       "<link href='http://fonts.googleapis.com/css?family=Source+Code+Pro:300,400' rel='stylesheet' type='text/css'>\n",
       "<style>\n",
       "\n",
       "@font-face {\n",
       "    font-family: \"Computer Modern\";\n",
       "    src: url('http://mirrors.ctan.org/fonts/cm-unicode/fonts/otf/cmunss.otf');\n",
       "}\n",
       "\n",
       "\n",
       "#notebook_panel { /* main background */\n",
       "    background: rgb(245,245,245);\n",
       "}\n",
       "\n",
       "div.cell { /* set cell width */\n",
       "    width: 750px;\n",
       "}\n",
       "\n",
       "div #notebook { /* centre the content */\n",
       "    background: #fff; /* white background for content */\n",
       "    width: 1000px;\n",
       "    margin: auto;\n",
       "    padding-left: 0em;\n",
       "}\n",
       "\n",
       "#notebook li { /* More space between bullet points */\n",
       "margin-top:0.8em;\n",
       "}\n",
       "\n",
       "/* draw border around running cells */\n",
       "div.cell.border-box-sizing.code_cell.running { \n",
       "    border: 1px solid #111;\n",
       "}\n",
       "\n",
       "/* Put a solid color box around each cell and its output, visually linking them*/\n",
       "div.cell.code_cell {\n",
       "    background-color: rgb(256,256,256); \n",
       "    border-radius: 0px; \n",
       "    padding: 0.5em;\n",
       "    margin-left:1em;\n",
       "    margin-top: 1em;\n",
       "}\n",
       "\n",
       "\n",
       "div.text_cell_render{\n",
       "    font-family: 'Alegreya Sans' sans-serif;\n",
       "    line-height: 140%;\n",
       "    font-size: 125%;\n",
       "    font-weight: 400;\n",
       "    width:600px;\n",
       "    margin-left:auto;\n",
       "    margin-right:auto;\n",
       "}\n",
       "\n",
       "/* Formatting for header cells */\n",
       ".text_cell_render h1 {\n",
       "    font-family: 'Alegreya Sans', sans-serif;\n",
       "    font-style:regular;\n",
       "    font-weight: 200;    \n",
       "    font-size: 50pt;\n",
       "    line-height: 100%;\n",
       "    color:#CD2305;\n",
       "    margin-bottom: 0.5em;\n",
       "    margin-top: 0.5em;\n",
       "    display: block;\n",
       "}\t\n",
       ".text_cell_render h2 {\n",
       "    font-family: 'Fenix', serif;\n",
       "    font-size: 22pt;\n",
       "    line-height: 100%;\n",
       "    margin-bottom: 0.1em;\n",
       "    margin-top: 0.3em;\n",
       "    display: block;\n",
       "}\t\n",
       "\n",
       ".text_cell_render h3 {\n",
       "    font-family: 'Fenix', serif;\n",
       "    margin-top:12px;\n",
       "\tfont-size: 16pt;\n",
       "    margin-bottom: 3px;\n",
       "    font-style: regular;\n",
       "}\n",
       "\n",
       ".text_cell_render h4 {    /*Use this for captions*/\n",
       "    font-family: 'Fenix', serif;\n",
       "    font-size: 2pt;\n",
       "    text-align: center;\n",
       "    margin-top: 0em;\n",
       "    margin-bottom: 2em;\n",
       "    font-style: regular;\n",
       "}\n",
       "\n",
       ".text_cell_render h5 {  /*Use this for small titles*/\n",
       "    font-family: 'Alegreya Sans', sans-serif;\n",
       "    font-weight: 300;\n",
       "    font-size: 16pt;\n",
       "    color: #CD2305;\n",
       "    font-style: italic;\n",
       "    margin-bottom: .5em;\n",
       "    margin-top: 0.5em;\n",
       "    display: block;\n",
       "}\n",
       "\n",
       ".text_cell_render h6 { /*use this for copyright note*/\n",
       "    font-family: 'Source Code Pro', sans-serif;\n",
       "    font-weight: 300;\n",
       "    font-size: 9pt;\n",
       "    line-height: 100%;\n",
       "    color: grey;\n",
       "    margin-bottom: 1px;\n",
       "    margin-top: 1px;\n",
       "}\n",
       "\n",
       "    .CodeMirror{\n",
       "            font-family: \"Source Code Pro\";\n",
       "\t\t\tfont-size: 90%;\n",
       "    }\n",
       "/*    .prompt{\n",
       "        display: None;\n",
       "    }*/\n",
       "\t\n",
       "    \n",
       "    .warning{\n",
       "        color: rgb( 240, 20, 20 )\n",
       "        }  \n",
       "</style>\n",
       "<script>\n",
       "    MathJax.Hub.Config({\n",
       "                        TeX: {\n",
       "                           extensions: [\"AMSmath.js\"], \n",
       "                           equationNumbers: { autoNumber: \"AMS\", useLabelIds: true}\n",
       "                           },\n",
       "                tex2jax: {\n",
       "                    inlineMath: [ ['$','$'], [\"\\\\(\",\"\\\\)\"] ],\n",
       "                    displayMath: [ ['$$','$$'], [\"\\\\[\",\"\\\\]\"] ]\n",
       "                },\n",
       "                displayAlign: 'center', // Change this to 'center' to center equations.\n",
       "                \"HTML-CSS\": {\n",
       "                    styles: {'.MathJax_Display': {\"margin\": 4}}\n",
       "                }\n",
       "        });\n",
       "</script>\n"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from IPython.core.display import HTML\n",
    "def css_styling(filepath):\n",
    "    styles = open(filepath, 'r').read()\n",
    "    return HTML(styles)\n",
    "css_styling('../styles/custom.css')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
